package com.study.subscriber;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Created by andys on 2017/12/10.
 */
public class MessageSubscriber {
    public static void main(String[] args) {
        ConnectionFactory connectionFactory;//连接工厂
        Connection connection=null;
        Session session;
        Destination destination;
        MessageConsumer messageConsumer;

        connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,ActiveMQConnection.DEFAULT_BROKER_URL);
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE);
            destination = session.createTopic("Topic1");
            messageConsumer = session.createConsumer(destination);
            //以Listener的方式接收消息
            messageConsumer.setMessageListener(new FirstListener());//注册消息监听
        } catch (JMSException e) {
            e.printStackTrace();
        }

    }
}
